package com.way.fileexplorer.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.BaseAdapter;

import com.way.fileexplorer.R;
import com.way.fileexplorer.activities.FilePasteActivity;
import com.way.fileexplorer.activities.FileSearchResultActivity;
import com.way.fileexplorer.adapters.CopyDestListAdapter;

public class ActivityUtils {
    
    private static String TAG = "ActivityUtils";

    public static void showDestSelectDialog(final Context context,
            DialogInterface.OnClickListener listener) {
        if (context != null) {
            BaseAdapter adapter = new CopyDestListAdapter();
            new AlertDialog.Builder(context)
            .setTitle(R.string.select_destination)
            .setAdapter(adapter, listener)
            .show();
        }
    }
    
    public static void startCopyActivity(Context context, boolean isCut, 
            boolean toSdcard, String copyFilePath) {
        Intent intent = new Intent(context, FilePasteActivity.class);
        int flag = 0;
        if(isCut) {
            flag |= FilePasteActivity.FLAG_CUT_OP;
        }
        if(toSdcard) {
            flag |= FilePasteActivity.FLAG_TO_SDCARD;
        }
        intent.putExtra(FilePasteActivity.PASTE_FLAG, flag);
        intent.putExtra(FilePasteActivity.PASTE_SOURCE_PATH, copyFilePath);
        context.startActivity(intent);
    }
    
    public static void startCopyActivity(Context context, boolean isCut, 
            boolean toSdcard, String[] copyFilePaths) {
        if(copyFilePaths.length == 0) {
            Log.w(TAG, "call startCopyActivity(), but copyFilePaths is empty");
            return;
        } else if(copyFilePaths.length == 1) {
            startCopyActivity(context, isCut, toSdcard, copyFilePaths[0]);
            return;
        }
        Intent intent = new Intent(context, FilePasteActivity.class);
        int flag = FilePasteActivity.FLAG_MULT_OP;
        if(isCut) {
            flag |= FilePasteActivity.FLAG_CUT_OP;
        }
        if(toSdcard) {
            flag |= FilePasteActivity.FLAG_TO_SDCARD;
        }
        intent.putExtra(FilePasteActivity.PASTE_FLAG, flag);
        intent.putExtra(FilePasteActivity.PASTE_SOURCE_PATH, copyFilePaths);
        context.startActivity(intent);
    }

    public static void startSearchMode(Bundle bun, Context context) {
        if (context != null && bun != null) {
            ((Activity) context).startActivity(new Intent().setClass(
                    context.getApplicationContext(),
                    FileSearchResultActivity.class).putExtra(
                    FileSearch.SEARCH_ATTACH, bun));
        }
    }

    public static class CopyFileListener implements DialogInterface.OnClickListener {

        private Context mStartContext;
        
        private boolean mIsCut;
        
        private List<String> mOpFilesPath = new ArrayList<String>();
        
        public CopyFileListener(Context startContext) {
            super();
            this.mStartContext = startContext;
        }

        public void setCut(boolean isCut) {
            mIsCut = isCut;
        }
        
        public void addOpFiles(List<File> files) {
            for(File f: files) {
                mOpFilesPath.add(f.getAbsolutePath());
            }
        }
        
        public void addOpFile(File file) {
            mOpFilesPath.add(file.getAbsolutePath());
        }
        
        @Override
        public void onClick(DialogInterface dialog, int which) {
            startCopyActivity(mStartContext, mIsCut, which > 0, mOpFilesPath.toArray(new String[0]));
            clear();
        }

        private void clear() {
            mIsCut = false;
            mOpFilesPath.clear();
        }
    }
}
